package com.study.customer.mq;

import com.rabbitmq.client.AMQP;
import com.study.customer.pojo.GameGoods;
import com.study.customer.pojo.NewGameGoods;
import com.study.customer.service.GameGoodsService;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class GoodsListener {

    @Autowired
    private GameGoodsService gameGoodsService;

    /**
     * 监听货品新增或修改
     * @param
     */
    @RabbitListener(queues = "goods.insert.queue")
    public void listenGoodsInsertOrUpdate(@Payload NewGameGoods newGameGoods) {
        GameGoods gameGoods = new GameGoods();
        gameGoods.setId(newGameGoods.getId());
        gameGoods.setGameName(newGameGoods.getGameName());
        gameGoods.setVersion(newGameGoods.getVersion());
        gameGoods.setPrice(Money.of(CurrencyUnit.of("CNY"), newGameGoods.getPrice()));
        gameGoodsService.addGoods(gameGoods);
    }


    @RabbitListener(queues = "goods.delete.queue")
    public void listenGoodsDelete(Long id){
        gameGoodsService.deleteById(id);
    }
}
